## setting up fire scar data in monthly x y tables that match climate grids.

####Load the libraries

#list the libraries needed
necessary=c("geoR", "caTools", "sp", "rgdal", "adehabitat", "maptools")

#check if library is installed
installed = necessary %in% installed.packages()
#if library is not installed, install it
if (length(necessary[!installed]) >=1) install.packages(necessary[!installed], dep = T)

#load the libraries
for (lib in necessary) library(lib,character.only=T)
    
##set the working directory

setwd ("C:/pyrogeography/DATA/")

outdir = ("C:/pyrogeography/DATA/outputs/")

#read in the fire scars and associated firescar data

FS0409 = read.csv ( "fireDATA.csv")
FS0409 [1:10,]   # look at the data
 
# create a data frame for 09 and convert signed 8 bit value to month.

FS09 = data.frame(FS0409$lat, FS0409$lon, FS0409$fs2009)
names(FS09) = c("lat", "lon", "grid")
FS09 [1:10,] # look at the data 

FS09$month = ifelse (FS09$grid > 0 & FS09$grid < 9, 1, FS09$grid)
FS09$month = ifelse (FS09$grid > 8 & FS09$grid < 17, 2, FS09$month)
FS09$month = ifelse (FS09$grid > 16 & FS09$grid < 25, 3, FS09$month)
FS09$month = ifelse (FS09$grid > 24 & FS09$grid < 37, 4, FS09$month)
FS09$month = ifelse (FS09$grid > 36 & FS09$grid < 56, 5, FS09$month)
FS09$month = ifelse (FS09$grid > 55 & FS09$grid < 86, 6, FS09$month)
FS09$month = ifelse (FS09$grid > 85 & FS09$grid < 116, 7, FS09$month)
FS09$month = ifelse (FS09$grid > 115 & FS09$grid < 146, 8, FS09$month)
FS09$month = ifelse (FS09$grid > 145 & FS09$grid < 176, 9, FS09$month)
FS09$month = ifelse (FS09$grid > 175 & FS09$grid < 206, 10, FS09$month)
FS09$month = ifelse (FS09$grid > 205 & FS09$grid < 232, 11, FS09$month)
FS09$month = ifelse (FS09$grid > 231 & FS09$grid < 255, 12, FS09$month)
FS09$month = ifelse (FS09$grid == 255, 5, FS09$month)

FS09  # see if it worked

write.csv (FS09,paste(outdir,"FS09.csv",sep=''),row.names=T)    ## write out the data

# create a data frame for 08 and convert signed 8 bit value to month.

FS08 = data.frame(FS0409$lat, FS0409$lon, FS0409$fs2008)
names(FS08) = c("lat", "lon", "grid")
FS08 [1:10,] # look at the data 

FS08$month = ifelse (FS08$grid > 0 & FS08$grid < 9, 1, FS08$grid)
FS08$month = ifelse (FS08$grid > 8 & FS08$grid < 17, 2, FS08$month)
FS08$month = ifelse (FS08$grid > 16 & FS08$grid < 25, 3, FS08$month)
FS08$month = ifelse (FS08$grid > 24 & FS08$grid < 37, 4, FS08$month)
FS08$month = ifelse (FS08$grid > 36 & FS08$grid < 56, 5, FS08$month)
FS08$month = ifelse (FS08$grid > 55 & FS08$grid < 86, 6, FS08$month)
FS08$month = ifelse (FS08$grid > 85 & FS08$grid < 116, 7, FS08$month)
FS08$month = ifelse (FS08$grid > 115 & FS08$grid < 146, 8, FS08$month)
FS08$month = ifelse (FS08$grid > 145 & FS08$grid < 176, 9, FS08$month)
FS08$month = ifelse (FS08$grid > 175 & FS08$grid < 206, 10, FS08$month)
FS08$month = ifelse (FS08$grid > 205 & FS08$grid < 232, 11, FS08$month)
FS08$month = ifelse (FS08$grid > 231 & FS08$grid < 256, 12, FS08$month)


FS08  # see if it worked

write.csv (FS08,paste(outdir,"FS08.csv",sep=''),row.names=T)    ## write out the data


 # #   FS 2007
FS07 = data.frame(FS0409$lat, FS0409$lon, FS0409$fs2007)
names(FS07) = c("lat", "lon", "grid")
FS07 [1:10,] # look at the data 

FS07$month = ifelse (FS07$grid > 0 & FS07$grid < 3, 1, FS07$grid)
FS07$month = ifelse (FS07$grid > 2 & FS07$grid < 5, 4, FS07$month)
FS07$month = ifelse (FS07$grid > 4 & FS07$grid < 8, 2, FS07$month)
FS07$month = ifelse (FS07$grid > 7 & FS07$grid < 11, 3, FS07$month)
FS07$month = ifelse (FS07$grid > 10 & FS07$grid < 16, 4, FS07$month)
FS07$month = ifelse (FS07$grid > 15 & FS07$grid < 46, 5, FS07$month)
FS07$month = ifelse (FS07$grid > 45 & FS07$grid < 76, 6, FS07$month)
FS07$month = ifelse (FS07$grid > 75 & FS07$grid < 106, 7, FS07$month)
FS07$month = ifelse (FS07$grid > 105 & FS07$grid < 136, 8, FS07$month)
FS07$month = ifelse (FS07$grid > 135 & FS07$grid < 166, 9, FS07$month)
FS07$month = ifelse (FS07$grid > 165 & FS07$grid < 196, 10, FS07$month)
FS07$month = ifelse (FS07$grid > 195 & FS07$grid < 226, 11, FS07$month)
FS07$month = ifelse (FS07$grid > 225 & FS07$grid < 256, 12, FS07$month)

FS07  # see if it worked

write.csv (FS07,paste(outdir,"FS07.csv",sep=''),row.names=T)    ## write out the data

## FS06

FS06 = data.frame(FS0409$lat, FS0409$lon, FS0409$fs2006)
names(FS06) = c("lat", "lon", "grid")
FS06 [1:10,] # look at the data 

FS06$month = ifelse (FS06$grid > 0 & FS06$grid < 5, 1, FS06$grid)
FS06$month = ifelse (FS06$grid > 4 & FS06$grid < 10, 7, FS06$month)
FS06$month = ifelse (FS06$grid > 9 & FS06$grid < 11, 1, FS06$month)
FS06$month = ifelse (FS06$grid > 10 & FS06$grid < 21, 2, FS06$month)
FS06$month = ifelse (FS06$grid > 20 & FS06$grid < 31, 3, FS06$month)
FS06$month = ifelse (FS06$grid > 30 & FS06$grid < 50, 4, FS06$month)
FS06$month = ifelse (FS06$grid > 49 & FS06$grid < 75, 5, FS06$month)
FS06$month = ifelse (FS06$grid > 74 & FS06$grid < 100, 6, FS06$month)
FS06$month = ifelse (FS06$grid > 99 & FS06$grid < 125, 7, FS06$month)
FS06$month = ifelse (FS06$grid > 124 & FS06$grid < 150, 8, FS06$month)
FS06$month = ifelse (FS06$grid > 149 & FS06$grid < 175, 9, FS06$month)
FS06$month = ifelse (FS06$grid > 174 & FS06$grid < 200, 10, FS06$month)
FS06$month = ifelse (FS06$grid > 199 & FS06$grid < 219, 11, FS06$month)
FS06$month = ifelse (FS06$grid > 218 & FS06$grid < 230, 12, FS06$month)

FS06  # see if it worked

write.csv (FS06,paste(outdir,"FS06.csv",sep=''),row.names=T)    ## write out the data

## FS05

FS05 = data.frame(FS0409$lat, FS0409$lon, FS0409$fs2005)
names(FS05) = c("lat", "lon", "grid")
FS05 [1:10,] # look at the data 

FS05$month = ifelse (FS05$grid > 0 & FS05$grid < 20, 1, FS05$grid)
FS05$month = ifelse (FS05$grid > 19 & FS05$grid < 30, 2, FS05$month)
FS05$month = ifelse (FS05$grid > 29 & FS05$grid < 40, 3, FS05$month)
FS05$month = ifelse (FS05$grid > 39 & FS05$grid < 50, 4, FS05$month)
FS05$month = ifelse (FS05$grid > 49 & FS05$grid < 60, 5, FS05$month)
FS05$month = ifelse (FS05$grid > 59 & FS05$grid < 70, 6, FS05$month)
FS05$month = ifelse (FS05$grid > 69 & FS05$grid < 86, 7, FS05$month)
FS05$month = ifelse (FS05$grid > 85 & FS05$grid < 102, 8, FS05$month)
FS05$month = ifelse (FS05$grid > 101 & FS05$grid < 118, 9, FS05$month)
FS05$month = ifelse (FS05$grid > 117 & FS05$grid < 134, 10, FS05$month)
FS05$month = ifelse (FS05$grid > 133 & FS05$grid < 149, 11, FS05$month)
FS05$month = ifelse (FS05$grid ==149, 12, FS05$month)
FS05$month = ifelse (FS05$grid == 150, 11, FS05$month)
FS05$month = ifelse (FS05$grid > 150 & FS05$grid < 154, 12, FS05$month)
FS05$month = ifelse (FS05$grid == 154, 11, FS05$month)
FS05$month = ifelse (FS05$grid > 154 & FS05$grid < 160, 12, FS05$month)

FS05  # see if it worked

write.csv (FS05,paste(outdir,"FS05.csv",sep=''),row.names=T)    ## write out the data


## FS04
FS04 = data.frame(FS0409$lat, FS0409$lon, FS0409$fs2004)
names(FS04) = c("lat", "lon", "month")

FS04

write.csv (FS04,paste(outdir,"FS04.csv",sep=''),row.names=T)  

############# if each month needs to be subsetted   #########################
## January 09

#FSJan09 [1:10,]
#FSJan09$month = ifelse (FSJan09$FS0409.fs2009 < 9, 1, NA)   #replace the raster value with the month
#FSJan09 [1:10,]      # see if it worked

## February 09
#FSFeb09 = subset(FS09, FS0409.fs2009 > 8 & FS0409.fs2009 < 17)  
#FSFeb09$month = ifelse (FSFeb09$FS0409.fs2009 > 8 & FSFeb09$FS0409.fs2009 < 17, 2, NA)    
#FSFeb09 [1:10,]      # see if it worked

## March 09

#FSmar09 = subset(FS09, FS0409.fs2009 > 16 & FS0409.fs2009 < 25)  
#FSmar09$month = ifelse (FSmar09$FS0409.fs2009 > 16 & FSmar09$FS0409.fs2009 < 25, 3, NA)    
#FSmar09 [1:10,]      # see if it worked

## April 09

#FSapr09 = subset(FS09, FS0409.fs2009 > 24 & FS0409.fs2009 < 37)  
#FSapr09$month = ifelse (FSapr09$FS0409.fs2009 > 24 & FSapr09$FS0409.fs2009 < 37, 4, NA)    
#FSapr09 [1:10,]      # see if it worked

## May 09

#FSapr09 = subset(FS09, FS0409.fs2009 > 24 & FS0409.fs2009 < 37)  
#FSapr09$month = ifelse (FSapr09$FS0409.fs2009 > 24 & FSapr09$FS0409.fs2009 < 37, 4, NA)    
#FSapr09 [1:10,]      # see if it worked